package com.movieSystem.view;

import com.movieSystem.model.UserInfo;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import tcpFirmware.OKClient;
import tcpFirmware.Response;
import com.movieSystem.model.Serial;
import tcpFirmware.Type;
import tcpFirmware.model.DBHelper;

import javax.naming.Name;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class MovieMoment extends Application {
    public Image img;
    public String name;
    public String username;
    public String center;
    String session;
    String marketTime="上架时间：";
    String price="价格：";
    TextArea textDetail;
    Stage self;






    MovieMoment(Image img1,String moviename){
        img=img1;
        name=moviename;
    }
    @Override
    public void start(Stage primaryStage) {
        try {

            Scene scene = new Scene(this.initScene(),1200,800);
            primaryStage.setResizable(false);   //窗口大小不可变
            primaryStage.setScene(scene);   //根节点
            primaryStage.show();    //展示

            self=primaryStage;
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    HashMap<String,String> nameList = new HashMap<>();

    public Parent initScene(){

        Font font50 = new Font(50);
        Font font20 = new Font(20);

//        System.out.println(name);
        //滚动面板
        ScrollPane scrollPane = new ScrollPane();
        VBox vbox = new VBox();
        scrollPane.setContent(vbox);
        vbox.setMaxWidth(1200);
        vbox.setMaxHeight(800);

        HBox hBox1 = new HBox();
        //电影海报
        ImageView imageView  = new ImageView(img);
        imageView.setFitWidth(280);
        imageView.setFitHeight(400);
        hBox1.getChildren().add(imageView);

        //电影介绍
        textDetail = new TextArea();
        textDetail.setEditable(false);
        textDetail.setWrapText(true);
        textDetail.setFont(font20);
//        hBox1.setAlignment(Pos.CENTER);//居中
//        hBox1.setText(Pos.TOP_CENTER); //居中
        hBox1.getChildren().add(textDetail);//detail
        vbox.getChildren().add(hBox1);

        //电影名称
        HBox hBox2 = new HBox();
        hBox2.setMinHeight(100);
//        hBox2.getChildren().add();//name
        Text text1 = new Text(name);
        text1.setFont(font50);
        hBox2.getChildren().add(text1);

        hBox2.setAlignment(Pos.TOP_CENTER); //居中
        vbox.getChildren().add(hBox2);




        //放映时间|电影场次|电影厅号

        try {
            OKClient okClient = new OKClient(new Response() {
                @Override
                public void onResponse(HashMap<String, String> header, byte[] dataBytes) {
                    ArrayList<HashMap<String,String>> data = Serial.byteToObject(dataBytes);
                    assert data != null;

                    Platform.runLater(()->{
                        //电影上架时间|电影价格|电影时长

                        marketTime = marketTime+header.get("marketTime");
                        price = price+header.get("price");
                        Text text = new Text();
                        text.setFont(font50);
                        text.setText(marketTime+"，"+price+"，"+"时长："+data.get(0).get("howLong"));

                        vbox.getChildren().add(text);

                        textDetail.setText(header.get("detail"));
                    });

//                    for()
                    for(HashMap<String ,String> map:data){
                        Platform.runLater(()->{
//                            HBox hBoxTemp = new HBox();
                            Button btnTemp = new Button("场次："+map.get("session")+"，影厅号："+map.get("center")+"，放映时间："+map.get("whenStart"));
                            btnTemp.setMinWidth(1200);
                            btnTemp.setOnMouseClicked((mouseEvent)->{
                                SeatChooser seatChooser = new SeatChooser(imageView.getImage(),name,map.get("session"),map.get("center"), header.get("price"),UserInfo.getUsername());
                                seatChooser.start(new Stage());

                            });

                            vbox.getChildren().add(btnTemp);
                        });

                    }
                }
            });

            okClient.setRoute("getAllMovie").setType(Type.text.type).setData("name",name);
            okClient.sendTo();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return scrollPane;
    }
}